package com.autumn.platform.auth.core.access.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.platform.auth.core.access.IPermission;
import com.autumn.platform.auth.core.access.IPermissionMapping;
import com.autumn.platform.core.context.reqeust.IRequest;
import com.autumn.platform.core.logger.Logs;

public class PermissionMappingComposite implements IPermissionMapping {

    @Autowired(required = false)
    private List<IPermissionMapping> mappings;

    public List<IPermissionMapping> getMappings() {
        return mappings;
    }

    public void setMappings(List<IPermissionMapping> mappings) {
        this.mappings = mappings;
    }

    @Override
    public IPermission lookup(IRequest request) {
        if (null != mappings) {
            for (IPermissionMapping mapping : mappings) {
                IPermission permission = mapping.lookup(request);
                if (null != permission) {
                    return permission;
                }
            }
        }
        Logs.warn("not found permission for request [id:" + request.getRequestId() + ", target:" + request.getTarget() + "]");
        return null;
    }
}
